package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.demo.entity.DTO.UserDTO;
import com.example.demo.entity.VO.FollowVO;
import com.example.demo.result.Result;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.example.demo.service.IFollowService;
import com.example.demo.entity.Follow;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 关注表 前端控制器
 * </p>
 *
 * @author Mr.we
 * @since 2024-06-04
 */
@Api(tags = "用户关注接口")
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Resource
    private IFollowService followService;

    @Resource
    private IUserService userService;

    @ApiOperationSupport(author = "Mr.xiayh")
    @ApiOperation(value = "以用户ID查询自己的关注接口")
    @GetMapping("/findFollowByUserId")
    public Result<UserDTO> findFollowByUserId(Integer userId) {
        // TODO 后期换成ToKen取到的ID来查询
        List<UserDTO> allByUserId = followService.findFollowByUserId(userId);
        return Result.success().msg("查询成功").data(allByUserId);
    }

    /**
     * 查询粉丝
     * @param userId
     * @return
     */
    @ApiOperationSupport(author = "Mr.xiayh")
    @ApiOperation(value = "以用户ID查询自己的粉丝接口")
    @GetMapping("/findFollowedByUserId")
    public Result<UserDTO> findFollowedByUserId(Integer userId) {
        // TODO 后期换成ToKen取到的ID来查询
        List<UserDTO> allByUserId = followService.findFollowedByUserId(userId);
        return Result.success().msg("查询成功").data(allByUserId);
    }

    // 取关
    @ApiOperationSupport(author = "Mr.xiayh")
    @ApiOperation(value = "取关接口")
    @DeleteMapping("/cancel")
    public Result<String> cancel(@RequestBody FollowVO followVO) {
        // TODO 后期换成ToKen取到的ID为取关者的ID
        Follow follow = new Follow();
        BeanUtil.copyProperties(followVO,follow);
        if (followService.cancelFollow(follow)) {
            return Result.success().msg("取关成功");
        }
        return Result.error().code(10003).msg("取关失败，没有关注该用户");
    }

    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "关注接口")
    @PostMapping("/add")
    public Result<String> add(@RequestBody FollowVO followVO) {
        // TODO 后期换成ToKen取到的ID为关注者的ID
        Follow follow = new Follow();
        BeanUtil.copyProperties(followVO,follow);
        //判断被关注着是否存在
        if (userService.findNicknameByUserId(follow.getFollowedUserId()) == null) {
            return Result.error().code(10003).msg("关注失败，被关注者不存在");
        }
        //判断是否重复关注
        if (followService.isHasFollow(follow)) {
            return Result.error().code(10003).msg("关注失败，重复关注");
        }
        if (followService.addFollow(follow)) {
            return Result.success().msg("添加成功");
        }
        return Result.error().msg("添加失败");
    }

/*    *//**
     * 查询所有接口
     * @return
     *//*
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "查询关注表所有数据")
    @GetMapping
    public Result<List<Follow>> findAll() {
        return Result.success(followService.list()).msg("查询成功");
    }

    *//**
     * 根据id查询数据接口
     * @param id
     * @return
     *//*
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @GetMapping("/{id}")
    public Result<Follow> findOne(@PathVariable Integer id) {
        return Result.success(followService.getById(id)).msg("查询成功");
    }

    *//**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     *//*
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",example="1",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页多少条",example="10",required = true)
    })
    @GetMapping("/page")
    public Result<MyPage<Follow>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        return Result.success().data(followService.page(new Page<>(pageNum, pageSize), queryWrapper)).msg("查询成功");
    }


    *//**
    * 新增
    * @param
    * @return
    *//*
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public Result<String> add(@RequestBody FollowVO followVO) {
        Follow follow = new Follow();
        BeanUtil.copyProperties(followVO,follow);
        followService.addFollow(follow);
        return Result.success().msg("添加成功");
    }

    *//**
     * 更新接口
     * @param
     * @return
     *//*
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "更新接口")
    @PutMapping
    public Result<String> save(@RequestBody Follow follow) {
        followService.saveOrUpdate(follow);
        return Result.success().msg("更新成功");
    }

    *//**
     * 删除接口
     * @param id
     * @return
     *//*
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        followService.removeById(id);
        return Result.success().msg("删除成功");
    }

    *//**
     * 批量删除接口
     * @param ids
     * @return
     *//*
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "批量删除接口")
    @PostMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        followService.removeByIds(ids);
        return Result.success().msg("批量删除成功");
    }*/
}